//package com.william.springsecurity.filter;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.william.springsecurity.common.Payload;
//import com.william.springsecurity.config.RsaKeyProperties;
//import com.william.springsecurity.pojo.SysUser;
//import com.william.springsecurity.utils.JwtUtils;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
//
//import javax.servlet.FilterChain;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.io.PrintWriter;
//import java.util.HashMap;
//import java.util.Map;
//
//public class JwtVerifyFilter extends BasicAuthenticationFilter {
//
//    private RsaKeyProperties prop;
//
//    public JwtVerifyFilter(AuthenticationManager authenticationManager, RsaKeyProperties prop) {
//        super(authenticationManager);
//        this.prop = prop;
//    }
//
//    public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
//        String header = request.getHeader("Authorization");
//        if (header == null || !header.startsWith("Bearer ")) {
//            //如果携带错误的token，则给用户提示请登录！
//            chain.doFilter(request, response);
//            response.setContentType("application/json;charset=utf-8");
//            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
//            PrintWriter out = response.getWriter();
//            Map resultMap = new HashMap();
//            resultMap.put("code", HttpServletResponse.SC_FORBIDDEN);
//            resultMap.put("msg", "请登录！");
//            out.write(new ObjectMapper().writeValueAsString(resultMap));
//            out.flush();
//            out.close();
//        } else {
//            //如果携带了正确格式的token要先得到token
//            String token = header.replace("Bearer ", "");
//            //验证tken是否正确
//            Payload<SysUser> payload = JwtUtils.getInfoFromToken(token, prop.getPublicKey(), SysUser.class);
//            SysUser user = payload.getUserInfo();
//            if(user!=null){
//                UsernamePasswordAuthenticationToken authResult = new UsernamePasswordAuthenticationToken(user.getUsername(), null, user.getAuthorities());
//                SecurityContextHolder.getContext().setAuthentication(authResult);
//                chain.doFilter(request, response);
//            }
//        }
//    }
//
//
//
//
//}
